package cn.com.elite.impl;

import java.sql.Types;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;

import com.elitecrm.saas.service.EliteSAASException;

public class GetKSLXService {
	private static final Log log = LogFactory.getLog(GetKSLXService.class);
	
	public String getKSLXService(DataSource dataSource,String ep_id,Document doc)throws Exception{
		String ReturnXml="";
		Element SchemeIDEle=(Element)doc.selectSingleNode("/Root/SchemeID");
		if(SchemeIDEle!=null){
			String SchemeID=SchemeIDEle.getText();
			if(SchemeID!=null){
				getKSLXServiceImpl(dataSource,ep_id,SchemeID);
			}else{
				throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"SchemeIDEle.getText()==null");
			}
		}else{
			throw new EliteSAASException(EliteSAASException.INVAILD_PARAMETER,"SchemeIDEle==null");
		}
		return ReturnXml;
	}
	public String getKSLXServiceImpl(DataSource dataSource,String ep_id,String SchemeID)throws Exception{
		String ReturnXml="";
		String W_ExamScheme="W_ExamScheme_"+ep_id;
        String sql="select SchemeName,SchemeDesc,VaildBegin,VaildEnd,VaildDateFlag,InType,ExamTime from "+W_ExamScheme+" where SchemeID =?";
		log.debug("getKSLXServiceImpl_sql:"+sql);
		Object[] params=new Object[]{SchemeID};  
		int[] types=new int[]{Types.VARCHAR};  
		List KSList=util.QueryPersonUseExecute(sql,types,params,dataSource);
		Document result=util.getDoc();
		Element root=result.addElement("R");
		root.addElement("result").addText("1");
		if(KSList!=null&&KSList.size()>0){
			root.addElement("SchemeLEN").addText(KSList.size()+"");
    		Map map=(Map)KSList.get(0);
    		String SchemeName=(String)map.get("SchemeName");
    		log.debug("SchemeName:"+SchemeName);
    		root.addElement("SchemeName").addText(SchemeName);
    		String SchemeDesc=(String)map.get("SchemeDesc");
    		log.debug("SchemeDesc:"+SchemeDesc);
    		root.addElement("SchemeDesc").addText(SchemeDesc);
    		String VaildBegin=(String)map.get("VaildBegin");
    		log.debug("VaildBegin:"+VaildBegin);
    		root.addElement("VaildBegin").addText(VaildBegin);
    		String VaildEnd=(String)map.get("VaildEnd");
    		log.debug("VaildEnd:"+VaildEnd);
    		root.addElement("VaildEnd").addText(VaildEnd);
    		String VaildDateFlag=(String)map.get("VaildDateFlag");
    		log.debug("VaildDateFlag:"+VaildDateFlag);
    		root.addElement("VaildDateFlag").addText(VaildDateFlag);
    		String InType=(String)map.get("InType");
    		log.debug("InType:"+InType);
    		root.addElement("InType").addText(InType);
    		String ExamTime=(String)map.get("ExamTime");
    		log.debug("ExamTime:"+ExamTime);
    		root.addElement("ExamTime").addText(ExamTime);
		}else{
			root.addElement("SchemeLEN").addText("0");
		}
		ReturnXml=result.asXML();
		log.debug("getKSLXServiceImpl_ReturnXml"+ReturnXml);
		return ReturnXml;
		
	}


}
